package com.fqgj.xjd.user.dao.impl;

import com.fqgj.common.api.Page;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.xjd.user.dao.TTaskCenterDao;
import com.fqgj.xjd.user.entity.TTaskCenterEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class TTaskCenterDaoImpl extends AbstractBaseMapper<TTaskCenterEntity> implements TTaskCenterDao {

    @Override
    public List<TTaskCenterEntity> selectListByStatusListAndTypeAndPage(List<Integer> statusList, int type, Page page) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", statusList);
        map.put("type", type);
        RowBounds rowBounds = new RowBounds(page.getStartIndex(), page.getPageSize());
        return getSqlSession().selectList(getStatement("selectListByStatusListAndTypeAndPage"), map, rowBounds);
    }

    @Override
    public List<TTaskCenterEntity> selectListByStatusAndTypeAndPage(Integer status, int type, Page page) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", status);
        map.put("type", type);
        RowBounds rowBounds = new RowBounds(page.getStartIndex(), page.getPageSize());
        return getSqlSession().selectList(getStatement("selectListByStatusAndTypeAndPage"), map, rowBounds);
    }

    @Override
    public List<TTaskCenterEntity> selectListByStatusAndPage(List<Integer> statusList, Page page) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", statusList);
        RowBounds rowBounds = new RowBounds(page.getStartIndex(), page.getPageSize());
        return getSqlSession().selectList(getStatement("selectListByStatusAndPage"), map, rowBounds);
    }

    @Override
    public void addTask(String userCode, String kycIdNumber, Integer taskType) {
        if (StringUtils.isBlank(userCode) || StringUtils.isBlank(kycIdNumber) || taskType == null) {
            return;
        }
        TTaskCenterEntity entity = new TTaskCenterEntity();
        entity.setTaskStatus(0);
        entity.setKycIdNumber(kycIdNumber);
        entity.setTaskRetryTimes(0);
        entity.setTaskType(taskType);
        entity.setUserCode(userCode);
        this.insert(entity);
    }

    @Override
    public void addTask(String userCode, String kycIdNumber, Integer taskType, String taskId, String req) {
        if (StringUtils.isBlank(userCode) || StringUtils.isBlank(kycIdNumber) || taskType == null) {
            return;
        }
        TTaskCenterEntity entity = new TTaskCenterEntity();
        entity.setTaskStatus(0);
        entity.setKycIdNumber(kycIdNumber);
        entity.setTaskId(taskId);
        entity.setTaskRetryTimes(0);
        entity.setTaskType(taskType);
        entity.setUserCode(userCode);
        entity.setReqParams(req);
        this.insert(entity);
    }

    @Override
    public void addFailTask(String userCode, Integer taskType, Integer taskStatus, String taskId) {
        if (StringUtils.isBlank(userCode) || StringUtils.isBlank(taskId) || taskType == null || taskStatus == null) {
            return;
        }
        TTaskCenterEntity entity = new TTaskCenterEntity();
        entity.setTaskStatus(2);
        entity.setKycIdNumber("");
        entity.setTaskId(taskId);
        entity.setTaskRetryTimes(0);
        entity.setTaskType(taskType);
        entity.setUserCode(userCode);
        entity.setReqParams("");
        this.insert(entity);
    }
}